package com.lntu.meeting.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lntu.meeting.pojo.MeetingRoom;
import com.lntu.meeting.pojo.Notes;
import com.lntu.meeting.service.ManageService;
import com.lntu.meeting.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/meetingManage")
public class manageController extends com.lntu.meeting.controller.BaseController {
    @Autowired
    private ManageService manageService;

    /**
     * 用户删除自己的会议室申请
     */
    @RequestMapping("deleteApply")
    public String  deleteApply(@RequestBody Notes notes){
        Integer userid=notes.getUserId();
        Integer id=notes.getId();
        String flag = "500";
        System.out.println("用户名："+userid+"密码："+id);
        /**影响条数不为空表示操作成功，修改响应码*/
        Integer integer=manageService.deleteNotesById(userid, id);
        if (integer!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        String res = JSON.toJSONString(resMap);
        return res;
    }

    /**
     * 管理员通过申请id批准预定信息
     */
    @RequestMapping("applyPermit")
    public String  applyPermit(@RequestBody Notes notes){
        Integer id=notes.getId();
        String flag = "500";
        Integer integer=manageService.applyById(id);
        if (integer!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        String res = JSON.toJSONString(resMap);
        return res;
    }

    /**
     * 管理员查询所有会议室预定信息
     */
    /*@RequestMapping("findAll")
    *//*public String findAll(@RequestParam("date") String date){*//*
    public String findAll(@RequestBody JSONObject data){
        String date=data.getString("data")+" "+"00:00:00";
        System.out.println(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1=null;
        try {
            date1 = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String flag = "500";
        System.out.println("date1"+date1);
        List<Pager> list=manageService.findAll(1,3,date1);
        if (list!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        resMap.put("date",list);
        String res = JSON.toJSONString(resMap);
        return res;
    }*/

    @RequestMapping("findAll")
    public String findAll(@RequestBody String data){
        String date=data.substring(0,data.length()-1)+" "+"00:00:00";
        System.out.println(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date1=null;
        String date2=null;
        try {
            date1 = format.parse(date);
            date2 = format.format(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String flag = "500";
        System.out.println("date1"+date1);
        System.out.println("date2"+date2);

        List<Pager> list=manageService.findAll(1,3,date1);
        if (list!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        resMap.put("date",list);
        String res = JSON.toJSONString(resMap);
        System.out.println("res"+res);
        return res;
    }

    /**
     * 管理员根据用户名或会议开始、结束时间查询会议室预定信息
     */
    @RequestMapping("find")
    public String find(Integer uid,Integer starttime,Integer endtime){
        String flag = "500";
        List<Notes> list=manageService.find(uid,starttime,endtime);
        if (list!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        resMap.put("date",list);
        String res = JSON.toJSONString(resMap);
        return res;
    }

    /**
     * 查询当前所有会议室
     */
    @RequestMapping("getAllRoom")
    public String findAllRoom(){
        String flag = "500";
        List<MeetingRoom> list=manageService.findAllRoom();
        if (list!=null){flag="200";}
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("flag", flag);
        resMap.put("date",list);
        String res = JSON.toJSONString(resMap);
        return res;
    }
}
